package com.ztc.util.excel;

import org.apache.poi.hssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * excel工具.
 */
public class ExcelUtil {
    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {
        // 第一步，创建一个webbook，对应一个Excel文件
        if (wb == null) {
            wb = new HSSFWorkbook();
        }
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow(0);

        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        HSSFCell cell = null;
        //创建标题
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }
        //创建内容
        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < values[i].length; j++) {
                row.createCell(j).setCellValue(values[i][j]);
            }
        }

        return wb;
    }

    /** 将文件存到指定位置 */
    public static void setResponseHeader(HttpServletResponse response, String fileName,HSSFWorkbook wb) {
        OutputStream os=null;
            try {
            setResponseHeader(response,fileName);
            os = response.getOutputStream();
            wb.write(os);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
          }
    }

    private static void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
        //准备输出
        response.reset();
        //设置response的Header
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        response.setContentType("application/octet-stream;charset=ISO8859-1");
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


    /** 公司钱包交易类型 */
    public static String getCapitalLogType(Integer type){
        if (type==null){
            return null;
        }
        switch (type){
            case 1:return "提现";
            case 2:return "提现失败";
            case 3:return "购票进账";
            case 4:return "购票进账";
            case 5:return "购票进账";
            case 6:return "购票进账";
            case 7:return "退票出账";
            case 8:return "退票出账";
            case 9:return "退票出账";
            case 10:return "退票出账";
            case 11:return "违约金进账";
            case 12:return "违约金进账";
            case 13:return "违约金进账";
            case 14:return "违约金进账";
            case 15:return "违约金出账";
            case 16:return "违约金出账";
            case 17:return "违约金出账";
            case 18:return "违约金出账";
            case 19:return "快递费收入";
            case 20:return "快递费支出";
            default:return null;
        }
    }

    /** 平台交易类型 */
    public static String getCapitalIncomeType(Integer type){
        if (type==null){
            return null;
        }
        switch (type){
            case 1:return "购票收入";
            case 2:return "退票支出";
            case 3:return "违约金收益";
            case 4:return "违约金出账";
            case 5:return "快递费收益";
            case 6:return "快递费出账";
            default:return null;
        }
    }
}
